//
//  TraineeViewController.m
//  TraineeManagementSystem
//
//  Created by Dhanashree Bodas on 23/01/15.
//  Copyright (c) 2015 Dhanashree Bodas. All rights reserved.
//

#import "TraineeViewController.h"

@interface TraineeViewController ()

@end

@implementation TraineeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self loadData];
    
      // [self.traineeTableview reloadData];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/



- (void)viewWillAppear:(BOOL)animated
{
    [self loadData];
    [self.traineeTableview reloadData];
    
}

- (IBAction)addTrainee:(id)sender {
    [self performSegueWithIdentifier:@"addTrainee" sender:nil];

}

-(void) loadData
{
    TraineeTable *t=[[TraineeTable alloc] init];
    
    self.tdata=[t listTrainee];

    [self.traineeTableview reloadData];
}



- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"currentAllocation"]) {
        
        
        
        NSIndexPath *indexPath = [self.traineeTableview indexPathForSelectedRow];
        Trainee *t=[self.tdata objectAtIndex:indexPath.row];
        int tid=t.tid;
        
        UITabBarController *tab=[segue destinationViewController];
                
                
                //For  tab 1
                CurrentAllocationViewController *ac=[tab.viewControllers objectAtIndex:0];
                ac.Title=@"Accepted";
                ac.self.tid=tid;
                ac.tabBarItem.image=[UIImage imageNamed:@"accept.png"];
                
                
                //For  tab 2
               RejectedCourseViewController *rc=[tab.viewControllers objectAtIndex:1];
                rc.Title=@"Rejected";
                rc.self.tid=tid;
                rc.tabBarItem.image=[UIImage imageNamed:@"reject.png"];
                
    }
}




#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tdata.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    Trainee *t1=[self.tdata objectAtIndex:[indexPath row]];
//   cell.textLabel.text=t1.fname;

    cell.textLabel.text=[t1.fname stringByAppendingFormat:@"   %@",t1.lname];
    return cell;
    
    
}


-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        //int recordIDToDelete = [[[self.tdata objectAtIndex:indexPath.row] objectAtIndex:0] intValue];
        
        Trainee * temp=[self.tdata objectAtIndex:[indexPath row]];
        int recordIDToDelete=temp.tid;
        TraineeTable *t=[[TraineeTable alloc] init];
        
        int cnt=[t deleteTrainee:recordIDToDelete];
       
        NSLog(@"cnt=%d",cnt);
        
        if(cnt>0)
        {
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Delete Trainee" message:@"Trainee Deleted successfully!!!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
            [alert show];
            
        }
 
        [self loadData];
    }
}

@end
